﻿using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for GridHelper
/// </summary>
public static class GridHelper
{
    public static void GridViewBind(GridView grv, object oDataSource)
    {
        grv.DataSource = oDataSource;
        grv.DataBind();
    }
    /// <summary>
    /// Verilen GridView'ı Excel'e import eder.
    /// Bu class'ın kullanıldığı sayfada
    /// public override void VerifyRenderingInServerForm(Control control){}
    /// Metodunun eklenmesi Gerekmektedir.
    /// </summary>
    /// <param name="Pg"></param>
    /// <param name="gv"></param>
    /// <param name="DosyaAd"></param>
    public static void GridViewToExcel(Page Pg, GridView gv, string DosyaAd)
    {
        Pg.Response.Clear();
        Pg.Response.Charset = "UTF-8";
        Pg.Response.ContentEncoding = System.Text.Encoding.Default;
        Pg.Response.AppendHeader("content-disposition", "attachment; filename=" + DosyaAd + ".xls");
        Pg.Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gv.RenderControl(htw);
        Pg.Response.Write(sw.ToString());
        Pg.Response.End();
    }
    /// <summary>
    /// Verilen GridView'ı Word'e import eder.
    /// Bu class'ın kullanıldığı sayfada
    /// public override void VerifyRenderingInServerForm(Control control){}
    /// Metodunun eklenmesi Gerekmektedir.
    /// </summary>
    /// <param name="Pg"></param>
    /// <param name="gv"></param>
    /// <param name="DosyaAd"></param>
    public static void GridViewToWord(Page Pg, GridView gv, string DosyaAd)
    {
        Pg.Response.Clear();
        Pg.Response.Charset = "UTF-8";
        Pg.Response.ContentEncoding = System.Text.Encoding.Default;
        Pg.Response.AppendHeader("content-disposition", "attachment; filename=" + DosyaAd + ".doc");
        Pg.Response.ContentType = "application/vnd.ms-word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gv.RenderControl(htw);
        Pg.Response.Write(sw.ToString());
        Pg.Response.End();
    }
}
